/**
 * Creates UI for Dockbay
 */
package View;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;

/**
 * This class holds and performs the dockbay actions (which currently is refueling)
 * @author Whitney
 * @version $Revision: 1.0 $
 */
public class DockbayWindow extends JPanel{
	
	/**
	 * Refuel and Exit Buttons 
	 */
	private final JButton refuelButton, exitButton;

	/**
	 * The integer array to be returned.
	 */
	private final int[] buttonPressed = new int[2];

	/**
	 * The background photo to be used.
	 */
	private final ImageIcon bayPicture;
	
	/**
	 * The handler for all the buttons.
	 */
	private final ButtonListener listen = new ButtonListener();
	
	/**
	 * This main method displays the amount of fuel, builds the refuel and cancel buttons
	 * and returns its button's values
	 * @param shipFuel	the amount of fuel that the ship is currently carrying
	 * @param fuelCapacity	the amount of fuel that the ship can hold
	 */
	public DockbayWindow(int shipFuel, int fuelCapacity) {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		bayPicture = new ImageIcon ("images/Dockbay.png");

//initialize buttonPressed to -1
		buttonPressed[0] = -1;
		
		final JLabel fuelLabel = new JLabel("Fuel: " + shipFuel + "/" + fuelCapacity);
		final JLabel warningLabel = new JLabel("");
		final int i = 23;
		final int y = 29;
		final int x = 150;
		fuelLabel.setBounds(i, 437, x, y);
		warningLabel.setBounds(i, 452, x, y);
		if(((fuelCapacity > 20) && shipFuel < 10) || shipFuel < (fuelCapacity >> 1)){
			fuelLabel.setForeground(Color.RED);
			warningLabel.setForeground(Color.RED);
			warningLabel.setText("Warning: Refuel");
			
		}
		else{
			fuelLabel.setForeground(Color.BLACK);
			warningLabel.setText("");
		}
		fuelLabel.setFont(new Font("Krungthep", Font.PLAIN, 15));
		warningLabel.setFont(new Font("Krungthep", Font.PLAIN, 10));
		add(fuelLabel);
		add(warningLabel);
		
		refuelButton = new JButton("Refuel");
		refuelButton.setBounds(132, 437, 117, 29);
		refuelButton.setFont(new Font("Krungthep", Font.PLAIN, 17));
		refuelButton.addActionListener(listen);
		add(refuelButton);
		
		exitButton = new JButton("Back to Ship");
		exitButton.setBounds(242, 437, 150, 29);
		exitButton.setFont(new Font("Krungthep", Font.PLAIN, 17));
		exitButton.addActionListener(listen);
		add(exitButton);
		
		final JLabel shipPicture = new JLabel("", bayPicture, SwingConstants.LEFT);
		shipPicture.setBounds(6, 6, 466, 427);
		shipPicture.setFont(new Font("Krungthep", Font.PLAIN, 17));
		add(shipPicture);
		
	}
	
	/**
	 * Returns the status of the buttons pressed
	 * 
	
	 * @return the value of the buttons pressed. 1 if refuel, 2 if exit */
	public int[] getButtonPressed(){
		return buttonPressed;
	}
	
	/**
	 * this class maintains all the listeners for the buttons on the page
	 * @author Whitney
	 *
	 */
	private class ButtonListener implements ActionListener{
		/**
		 * This method checks all the actions performed.
		 * @param event The event that occured
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed (ActionEvent event){
			if(event.getSource().equals(refuelButton)){
				buttonPressed[0] = 1;
			}
			if(event.getSource().equals(exitButton)){
				buttonPressed[0] = 2;
			}
		}

		/**
		 * The toString for the ButtonListener
		
		 * @return Returns a space because the button listener doesn't need a toString. */
		public String toString(){
			return "";
			
		}
	}
}

